﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Resources;

namespace TripLOA.GUI.roles
{
    public partial class ManageRoles : TripLOA.GUI.internationalization.LOABasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                DisplayRolesInGrid();
        }

        #region CREATE_ROLE
        /// <summary>
        /// Code behind the button that adds a new Role
        /// </summary>
        protected void but_createRole_Click(object sender, EventArgs e)
        {
            string newRoleName = txt_createRole.Text.Trim();

            if (newRoleName.Length > 0 && !newRoleName.Contains(','))
            {
                if (Roles.RoleExists(newRoleName))
                {
                   string rolepresent = this.GetLocalResourceObject("RolePresent.Text").ToString();
                   lab_confirm.Text = rolepresent;
                }
                else
                {
                    // Create the role
                    Roles.CreateRole(newRoleName);

                    // Refresh the RoleList Grid    
                    DisplayRolesInGrid();

                    // Set confirm label
                    string roleadded = this.GetLocalResourceObject("RoleAdded.Text").ToString();
                    lab_confirm.Text = roleadded;
                }
            }            
            else
            {
                string roleinvalid = this.GetLocalResourceObject("RoleInvalid.Text").ToString();
                lab_confirm.Text = roleinvalid;            
            }

            txt_createRole.Text = string.Empty;
        }
        #endregion CREATE_ROLE

        #region ROLES_LIST

        /// <summary>
        /// displays the roles configured in the MembershipProvider
        /// </summary>
        private void DisplayRolesInGrid()
        {
            RoleGrid.DataSource = Roles.GetAllRoles();
            RoleGrid.DataBind();
        }

        /// <summary>
        /// manages the deletion of a role in the displayed roles' grid
        /// </summary>
        protected void RoleGrid_RoleDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get the RoleNameLabel
            Label lab_role = RoleGrid.Rows[e.RowIndex].FindControl("lab_role") as Label;

            // Delete the role
            Roles.DeleteRole(lab_role.Text, false);

            // Change lab_confirm
            string roledeleted = this.GetLocalResourceObject("RoleDeleted.Text").ToString(); ;
            lab_confirm.Text = roledeleted;

            // Refresh the data 
            DisplayRolesInGrid();
        }       

        #endregion ROLES_LIST   

    }

}
    

